package com.sneakxy.cloudbase.platform.sentinel;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Proxy;

import org.springframework.cloud.openfeign.FallbackFactory;

import com.sneakxy.cloudbase.platform.utils.web.Response;

public abstract class CloudBaseFallBackFactory<T> implements FallbackFactory<T> {

	@Override
	@SuppressWarnings("unchecked")
	public T create(Throwable cause) {
		Class<T> clazz = getClientClass();
		return (T) Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[] {clazz}, new InvocationHandler() {
			@Override
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
				return Response.fail(500, "服务未开启，降级处理");
			}
		});
	}
	
	@SuppressWarnings("unchecked")
	public Class<T> getClientClass() {
		return (Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
	}

}
